package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.common.constant.SysUserStatusEnum;
import com.atguigu.spzx.common.utils.BeanUtils;
import com.atguigu.spzx.common.utils.MD5;
import com.atguigu.spzx.manager.mapper.SysRoleMapper;
import com.atguigu.spzx.manager.mapper.SysUserMapper;
import com.atguigu.spzx.manager.mapper.SysUserRoleMapper;
import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.manager.entity.SysRole;
import com.atguigu.spzx.common.entity.SysUser;
import com.atguigu.spzx.manager.entity.SysUserRole;
import com.atguigu.spzx.manager.vo.sysUser.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * SysUserService
 * </p>
 *
 * @author flyduck
 * @since 2023/10/25
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 条件分页查询
     * @param pageParam
     * @param sysUserQueryReqVo
     * @return
     */
    @Override
    public Page<SysUserQueryRespVo> getUserPageByQuery(Page<SysUser> pageParam, SysUserQueryReqVo sysUserQueryReqVo) {
        Page<SysUserQueryRespVo> page = sysUserMapper.getUserPageByQuery(pageParam,sysUserQueryReqVo);
        return page;
    }

    /**
     * 添加用户
     * @param sysUserSaveReqVo
     * @return
     */
    @Override
    public void saveUser(SysUserSaveReqVo sysUserSaveReqVo) {
        SysUser sysUser = BeanUtils.toBean(sysUserSaveReqVo, SysUser.class);
        //输入密码进行加密
        sysUser.setPassword(MD5.encrypt(sysUser.getPassword()));
        sysUser.setStatus(SysUserStatusEnum.NORMAL.getCode());
        sysUserMapper.insert(sysUser);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public SysUserQueryRespVo getUserById(Long id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        SysUserQueryRespVo sysUserQueryRespVo = BeanUtils.toBean(sysUser, SysUserQueryRespVo.class);
        return sysUserQueryRespVo;
    }

    /**
     * 修改用户
     * @param sysUserUpdateReqVo
     * @return
     */
    @Override
    public void updateUserById(SysUserUpdateReqVo sysUserUpdateReqVo) {
        SysUser sysUser = BeanUtils.toBean(sysUserUpdateReqVo, SysUser.class);
        sysUserMapper.updateById(sysUser);
    }

    /**
     * 根据id逻辑删除用户
     * @param id
     * @return
     */
    @Override
    public void deleteUserById(Long id) {
        sysUserMapper.deleteById(id);
    }

    /**
     * 更改用户状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatusById(Long id, Integer status) {
        SysUser sysUser = sysUserMapper.selectById(id);
        sysUser.setStatus(status);
        sysUserMapper.updateById(sysUser);
    }

    /**
     * 获取用户的角色数据
     * @param userId
     * @return
     */
    @Override
    public SysUserAssignRoleQueryRespVo getAssignedRoleListByUserId(Long userId) {
        SysUserAssignRoleQueryRespVo respVo = new SysUserAssignRoleQueryRespVo();
        //获取所有角色
        List<SysRole> allRoles = sysRoleMapper.selectList(null);
        //根据用户id查询分配的角色
        List<SysUserRole> sysUserRoleList = sysUserRoleMapper.findByUserId(userId);
        List<Long> roleIdList = sysUserRoleList.stream()
                .map(SysUserRole::getRoleId)
                .collect(Collectors.toList());

        respVo.setAllRoles(allRoles);
        respVo.setUserRoleIds(roleIdList);
        return respVo;
    }

    /**
     * 用户分配角色
     * @param reqVo
     */
    @Override
    @Transactional
    public void assignRoleForUser(SysUserAssignRoleReqVo reqVo) {
        //根据用户id删除之前的角色
        sysUserRoleMapper.deleteByUserId(reqVo.getUserId());

        //获取角色id，添加用户角色关系表
        List<Long> roleIdList = reqVo.getRoleIdList();
        sysUserRoleMapper.insertList(reqVo.getUserId(),roleIdList);
    }
}
